﻿using Dora.Orm.SqlSugarCore.UnitOfWork;
using Dora.Services.AdminUser.Dto.QueryDto;
using Dora.Services.AdminUser.Dto.ViewDto;
using Dora.Services.AdminUser.Entities;
using Dora.Tools.Enums.AdminUser;
using SqlSugar;
using Surging.Core.Common;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Dora.Services.AdminUser.Repository.Repositorys
{
    /// <summary>
    /// 权限表仓储层
    /// </summary>
    public class PermissionRepository : SqlSugarRepository<PermissionEntity, int>
    {
        private ISugarUnitOfWork _unitOfWork;
        public PermissionRepository(ISugarUnitOfWork unitOfWork) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        #region 通用权限相关
        public async Task<int> GetMaxId()
        {
            return await _unitOfWork.QueryClient.Queryable<PermissionEntity>().MaxAsync(it => it.Id);
        }
        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageDataList<PermissionEntity>> GetPageList(PermissionQueryDto input)
        {
            var pagePredict = _unitOfWork.QueryClient.Queryable<PermissionEntity>()
                .Where(p => p.IsDeleted == 0);
            var totalCount = await pagePredict.CountAsync();
            var pageList = await pagePredict.OrderBy(it => it.Id, OrderByType.Desc)
                .ToPageListAsync(input.PageIndex, input.PageSize);
            var page = new PageDataList<PermissionEntity>(totalCount, pageList, input.PageIndex, input.PageSize);
            return page;
        }

        public async Task<List<PermissionListDto>> GetListBySystemIdList(List<int> systemIdList)
        {
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity>()
                .Where(p => p.IsDeleted == 0)
                .Where(it => systemIdList.Contains(it.SystemId))
                .OrderBy(it => it.Sort, OrderByType.Desc)
                .OrderBy(it => it.CreatedTime, OrderByType.Desc)
                .Select(it => new PermissionListDto()
                {
                    Id = it.Id,
                    ParentId = it.ParentId,
                    SystemId = it.SystemId,
                    SystemKey = it.SystemKey,
                    LevelIdx = it.LevelIdx,
                    LevelPath = it.LevelPath,
                    Name = it.Name,
                    PType = it.PType,
                    HasSub = it.HasSub,
                    AuthKey = it.AuthKey,
                    Sort = it.Sort,
                    Remark = it.Remark,
                    OrgType = it.OrgType,
                    Icon = it.Icon
                })
                .ToListAsync();
            return list;
        }

        /// <summary>
        /// 根据系统和orgType(传入的或通用的)获取权限列表，
        /// </summary>
        /// <param name="systemIdList"></param>
        /// <param name="orgType"></param>
        /// <returns></returns>
        public async Task<List<PermissionListDto>> GetListBySystemIdListAndOrgType(List<int> systemIdList,PermissionOrgType orgType)
        {
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity>()
                .Where(p => p.IsDeleted == 0)
                .Where(it => systemIdList.Contains(it.SystemId))
                .Where(it => it.OrgType == (int)orgType || it.OrgType == (int)PermissionOrgType.Common)
                .OrderBy(it => it.Sort, OrderByType.Desc)
                .OrderBy(it => it.CreatedTime, OrderByType.Desc)
                .Select(it => new PermissionListDto()
                {
                    Id = it.Id,
                    ParentId = it.ParentId,
                    SystemId = it.SystemId,
                    SystemKey = it.SystemKey,
                    LevelIdx = it.LevelIdx,
                    LevelPath = it.LevelPath,
                    Name = it.Name,
                    PType = it.PType,
                    HasSub = it.HasSub,
                    AuthKey = it.AuthKey,
                    Sort = it.Sort,
                    Remark = it.Remark,
                    OrgType = it.OrgType
                })
                .ToListAsync();
            return list;
        }

        /// <summary>
        /// 获取角色拥有的权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task<List<PermissionListDto>> GetByRoleId(Guid roleId)
        {
            //return Db.Queryable<EmployeePermission>().Where(p =>  p.IsDeleted == 0 && p.Id != SystemId).OrderBy(p => p.Sort, OrderByType.Desc).ToListAsync();
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity, RolePermissionMapEntity>((ep, erp) => new object[] { JoinType.Inner, ep.Id == erp.PermissionId })
                .Where((ep, erp) => erp.RoleId == roleId && ep.IsDeleted == 0)
                .Select((ep, erp) => new PermissionListDto()
                {
                    Id = ep.Id,
                    ParentId = ep.ParentId,
                    SystemId = ep.SystemId,
                    SystemKey = ep.SystemKey,
                    LevelIdx = ep.LevelIdx,
                    LevelPath = ep.LevelPath,
                    Name = ep.Name,
                    PType = ep.PType,
                    HasSub = ep.HasSub,
                    AuthKey = ep.AuthKey,
                    Sort = ep.Sort,
                    Remark = ep.Remark
                }).ToListAsync();
            return list;
        }

        public async Task<List<PermissionListDto>> GetBySystemId(int systemId)
        {
            return await _unitOfWork.QueryClient.Queryable<PermissionEntity>()
                .Where(p => p.IsDeleted == 0 && p.SystemId == systemId)
                .OrderBy(p => p.Sort, OrderByType.Desc)
                .Select(it => new PermissionListDto()
                {
                    Id = it.Id,
                    ParentId = it.ParentId,
                    SystemId = it.SystemId,
                    SystemKey = it.SystemKey,
                    LevelIdx = it.LevelIdx,
                    LevelPath = it.LevelPath,
                    Name = it.Name,
                    PType = it.PType,
                    HasSub = it.HasSub,
                    AuthKey = it.AuthKey,
                    Sort = it.Sort,
                    Remark = it.Remark
                })
                .ToListAsync();
        }

        public async Task<bool> UpdateHasSub(PermissionEntity entity)
        {
            var result = await _unitOfWork.ModifyClient.Updateable<PermissionEntity>()
                .UpdateColumns(it => new PermissionEntity() { HasSub = entity.HasSub, UpdatedTime = DateTime.Now })
                .Where(it => it.Id == entity.Id).ExecuteCommandAsync() > 0;
            return result;
            //var result = _unitOfWork.ModifyClient.Update(it => new EmployeePermission() { HasSub = entity.HasSub, UpdatedTime = DateTime.Now }, it => it.Id == entity.Id);
            //return result;
        }

        public async Task<bool> UpdateAuthKey(PermissionEntity entity)
        {
            var result = await _unitOfWork.ModifyClient.Updateable(entity).UpdateColumns(it => new { it.AuthKey }).ExecuteCommandAsync() > 0;
            return result;
        }

        public async Task<bool> UpdateLevelPath(PermissionEntity entity)
        {
            var result = await _unitOfWork.ModifyClient.Updateable(entity).UpdateColumns(it => new { it.LevelPath }).ExecuteCommandAsync() > 0;
            return result;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public new async Task<bool> Update(PermissionEntity entity)
        {
            var result = await _unitOfWork.ModifyClient.Updateable(entity).UpdateColumns(it => new {
                //it.Id,
                //it.ParentId,
                //it.SystemId,
                //it.SystemKey,
                //it.LevelIdx,
                //it.LevelPath,
                it.Name,
                it.Icon,
                //it.PType,
                //it.AuthKey,
                it.HasSub,
                it.AuthValue,
                it.Sort,
                it.Remark,
                //it.IsDeleted,
                //it.CreatedUserId,
                //it.CreatedUserName,
                //it.CreatedTime,
                //it.UpdatedUserId,
                //it.UpdatedUserName,
                //it.UpdatedTime,
            }).ExecuteCommandAsync() > 0;
            return result;
        }

        public new async Task<bool> Delete(PermissionEntity entity)
        {
            entity.IsDeleted = 1;
            entity.UpdatedTime = DateTime.Now;
            var result = await _unitOfWork.ModifyClient.Updateable(entity).UpdateColumns(it => new { it.IsDeleted, it.UpdatedUserId, it.UpdatedUserName, it.UpdatedTime }).ExecuteCommandAsync() > 0;

            //var result = Db.Updateable(entity).UpdateColumns(it => new { it.IsDeleted, it.UpdatedById, it.UpdatedBy, it.UpdatedTime }).ExecuteCommand() > 0;
            return result;
        }
        public async Task<bool> HasSub(int id)
        {
            var result = await _unitOfWork.QueryClient.Queryable<PermissionEntity>().Where(p => p.ParentId == id && p.IsDeleted == 0).CountAsync() > 0;
            return result;
        }
        #endregion

        #region 美程用户相关

        /// <summary>
        /// 获取美程用户拥有的所有权限
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="SystemId"></param>
        /// <returns></returns>
        public async Task<List<PermissionEntity>> GetDoraAuthedListByUserId(Guid userId)
        {
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity, RolePermissionMapEntity, RoleEntity, DoraUserRoleMapEntity>((p, rpm, r, urm) => new object[] {
                JoinType.Inner, p.Id == rpm.PermissionId
                ,JoinType.Inner, rpm.RoleId == r.Id
                ,JoinType.Inner, r.Id == urm.RoleId
            })
                .Where((p, rpm, r, urm) => urm.UserId == userId && p.IsDeleted == 0 && r.IsDeleted == 0 && r.IsEnabled == (int)RoleIsEnabled.Enabled)
                .OrderBy((p, rpm, r, urm) => p.Sort, OrderByType.Desc)
                .Select((p, rpm, r, urm) => p).Distinct().ToListAsync();
            return list;
        }

        /// <summary>
        /// 获取美程用户某个菜单下拥有的所有权限
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="MenuId"></param>
        /// <returns></returns>
        public async Task<List<PermissionEntity>> GetDoraAuthedListByMenuId(Guid UserId, int MenuId)
        {
            //return Db.Queryable<EmployeePermission>().Where(p =>  p.IsDeleted == 0 && p.Id != SystemId).OrderBy(p => p.Sort, OrderByType.Desc).ToListAsync();
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity, RolePermissionMapEntity, RoleEntity, DoraUserRoleMapEntity>((p, rpm, r, urm) => new object[] {
                JoinType.Inner, p.Id == rpm.PermissionId
                ,JoinType.Inner, rpm.RoleId == r.Id
                ,JoinType.Inner, r.Id == urm.RoleId
            })
                .Where((p, rpm, r, urm) => urm.UserId == UserId && p.ParentId == MenuId && p.IsDeleted == 0 && r.IsDeleted == 0 && r.IsEnabled == (int)RoleIsEnabled.Enabled)
                .OrderBy((p, rpm, r, urm) => p.Sort, OrderByType.Desc)
                .Select((ep, erp, er, erm) => ep).ToListAsync();
            return list;
        }
        /// <summary>
        /// 通过角色IdList获取权限列表
        /// </summary>
        /// <param name="roleIdList"></param>
        /// <returns></returns>
        public async Task<List<PermissionEntity>> GetDoraListByRoleIdList(List<Guid> roleIdList)
        {
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity, RolePermissionMapEntity, RoleEntity, DoraUserRoleMapEntity>((p, rpm, r, urm) => new object[] {
                JoinType.Inner, p.Id == rpm.PermissionId
                ,JoinType.Inner, rpm.RoleId == r.Id
                ,JoinType.Inner, r.Id == urm.RoleId
            })
               .Where((p, rpm, r, urm) => roleIdList.Contains(urm.RoleId) && p.IsDeleted == 0 && r.IsDeleted == 0 && r.IsEnabled == (int)RoleIsEnabled.Enabled)
               .OrderBy((p, rpm, r, urm) => p.Sort, OrderByType.Desc)
               .Select((p, rpm, r, urm) => p).ToListAsync();
            return list;
        }

        /// <summary>
        /// 获取美程用户某个系统下拥有的所有权限
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="systemId"></param>
        /// <returns></returns>
        public async Task<List<PermissionListDto>> GetDoraAuthedListBySystemId(Guid userId, int systemId)
        {
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity, RolePermissionMapEntity, RoleEntity, DoraUserRoleMapEntity>((p, rpm, r, urm) => new object[] {
                JoinType.Inner, p.Id == rpm.PermissionId
                ,JoinType.Inner, rpm.RoleId == r.Id
                ,JoinType.Inner, r.Id == urm.RoleId
            })
                .Where((p, rpm, r, urm) => urm.UserId == userId && p.SystemId == systemId && p.IsDeleted == 0 && r.IsDeleted == 0 && r.IsEnabled == (int)RoleIsEnabled.Enabled)
                .OrderBy((p, rpm, r, urm) => p.Sort, OrderByType.Desc)
                .Select((p, rpm, r, urm) => new PermissionListDto()
                {
                    Id = p.Id,
                    ParentId = p.ParentId,
                    SystemId = p.SystemId,
                    SystemKey = p.SystemKey,
                    LevelIdx = p.LevelIdx,
                    LevelPath = p.LevelPath,
                    Name = p.Name,
                    PType = p.PType,
                    HasSub = p.HasSub,
                    AuthKey = p.AuthKey,
                    Sort = p.Sort,
                    Remark = p.Remark
                }).ToListAsync();
            return list;
        }
        #endregion

        #region 供应商用户相关
        /// <summary>
        /// 获取供应商用户拥有的所有权限
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="SystemId"></param>
        /// <returns></returns>
        public async Task<List<PermissionEntity>> GetSrmAuthedListByUserId(Guid userId)
        {
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity, RolePermissionMapEntity, RoleEntity, SrmUserRoleMapEntity>((p, rpm, r, urm) => new object[] {
                JoinType.Inner, p.Id == rpm.PermissionId
                ,JoinType.Inner, rpm.RoleId == r.Id
                ,JoinType.Inner, r.Id == urm.RoleId
            })
                .Where((p, rpm, r, urm) => urm.UserId == userId && p.IsDeleted == 0 && r.IsDeleted == 0 && r.IsEnabled == (int)RoleIsEnabled.Enabled)
                .OrderBy((p, rpm, r, urm) => p.Sort, OrderByType.Desc)
                .Select((p, rpm, r, urm) => p).Distinct().ToListAsync();
            return list;
        }

        /// <summary>
        /// 获取供应商用户某个菜单下拥有的所有权限
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="MenuId"></param>
        /// <returns></returns>
        public async Task<List<PermissionEntity>> GetSrmAuthedListByMenuId(Guid UserId, int MenuId)
        {
            //return Db.Queryable<EmployeePermission>().Where(p =>  p.IsDeleted == 0 && p.Id != SystemId).OrderBy(p => p.Sort, OrderByType.Desc).ToListAsync();
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity, RolePermissionMapEntity, RoleEntity, SrmUserRoleMapEntity>((p, rpm, r, urm) => new object[] {
                JoinType.Inner, p.Id == rpm.PermissionId
                ,JoinType.Inner, rpm.RoleId == r.Id
                ,JoinType.Inner, r.Id == urm.RoleId
            })
                .Where((p, rpm, r, urm) => urm.UserId == UserId && p.ParentId == MenuId && p.IsDeleted == 0 && r.IsDeleted == 0 && r.IsEnabled == (int)RoleIsEnabled.Enabled)
                .OrderBy((p, rpm, r, urm) => p.Sort, OrderByType.Desc)
                .Select((p, rpm, r, urm) => p).ToListAsync();
            return list;
        }

        /// <summary>
        /// 通过角色IdList获取权限列表
        /// </summary>
        /// <param name="roleIdList"></param>
        /// <returns></returns>
        public async Task<List<PermissionEntity>> GetSrmListByRoleIdList(List<Guid> roleIdList)
        {
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity, RolePermissionMapEntity, RoleEntity, SrmUserRoleMapEntity>((p, rpm, r, urm) => new object[] {
                JoinType.Inner, p.Id == rpm.PermissionId
                ,JoinType.Inner, rpm.RoleId == r.Id
                ,JoinType.Inner, r.Id == urm.RoleId
            })
               .Where((p, rpm, r, urm) => roleIdList.Contains(urm.RoleId) && p.IsDeleted == 0 && r.IsDeleted == 0 && r.IsEnabled == (int)RoleIsEnabled.Enabled)
               .OrderBy((p, rpm, r, urm) => p.Sort, OrderByType.Desc)
               .Select((p, rpm, r, urm) => p).ToListAsync();
            return list;
        }

        /// <summary>
        /// 获取供应商用户某个系统下拥有的所有权限
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="systemId"></param>
        /// <returns></returns>
        public async Task<List<PermissionListDto>> GetSrmAuthedListBySystemId(Guid userId, int systemId)
        {
            //return Db.Queryable<EmployeePermission>().Where(p =>  p.IsDeleted == 0 && p.Id != SystemId).OrderBy(p => p.Sort, OrderByType.Desc).ToListAsync();
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity, RolePermissionMapEntity, RoleEntity, SrmUserRoleMapEntity>((p, rpm, r, urm) => new object[] {
                JoinType.Inner, p.Id == rpm.PermissionId
                ,JoinType.Inner, rpm.RoleId == r.Id
                ,JoinType.Inner, r.Id == urm.RoleId
            })
                .Where((p, rpm, r, urm) => urm.UserId == userId && p.SystemId == systemId && p.IsDeleted == 0 && r.IsDeleted == 0 && r.IsEnabled == (int)RoleIsEnabled.Enabled)
                .OrderBy((p, rpm, r, urm) => p.Sort, OrderByType.Desc)
                .Select((p, rpm, r, urm) => new PermissionListDto()
                {
                    Id = p.Id,
                    ParentId = p.ParentId,
                    SystemId = p.SystemId,
                    SystemKey = p.SystemKey,
                    LevelIdx = p.LevelIdx,
                    LevelPath = p.LevelPath,
                    Name = p.Name,
                    PType = p.PType,
                    HasSub = p.HasSub,
                    AuthKey = p.AuthKey,
                    Sort = p.Sort,
                    Remark = p.Remark
                }).ToListAsync();
            return list;
        }
        #endregion

        #region TAS用户相关
        /// <summary>
        /// 获取Tas用户拥有某个组织机构的所有权限
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="orgId"></param>
        /// <returns></returns>
        public async Task<List<PermissionEntity>> GetTasAuthedListByUserIdAndOrgId(Guid userId, int orgId)
        {
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity, RolePermissionMapEntity, RoleEntity, TasUserRoleMapEntity>((p, rpm, r, urm) => new object[] {
                JoinType.Inner, p.Id == rpm.PermissionId
                ,JoinType.Inner, rpm.RoleId == r.Id
                ,JoinType.Inner, r.Id == urm.RoleId
            })
                .Where((p, rpm, r, urm) => urm.UserId == userId && urm.OrgId==orgId && p.IsDeleted == 0 && r.IsDeleted == 0 && r.IsEnabled == (int)RoleIsEnabled.Enabled)
                .OrderBy((p, rpm, r, urm) => p.Sort, OrderByType.Desc)
                .Select((p, rpm, r, urm) => p).Distinct().ToListAsync();
            return list;
        }

        /// <summary>
        /// 获取Tas用户拥有权限的一个组织机构Id
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="orgId"></param>
        /// <returns></returns>
        public async Task<int> GetTasAuthedOrgId(Guid userId)
        {
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity, RolePermissionMapEntity, RoleEntity, TasUserRoleMapEntity>((p, rpm, r, urm) => new object[] {
                JoinType.Inner, p.Id == rpm.PermissionId
                ,JoinType.Inner, rpm.RoleId == r.Id
                ,JoinType.Inner, r.Id == urm.RoleId
            })
                .Where((p, rpm, r, urm) => urm.UserId == userId && p.IsDeleted == 0 && r.IsDeleted == 0 && r.IsEnabled == (int)RoleIsEnabled.Enabled)
                .OrderBy((p, rpm, r, urm) => p.Sort, OrderByType.Desc)
                .Select((p, rpm, r, urm) => urm.OrgId).FirstAsync();
            return list;
        }

        /// <summary>
        /// 获取Tas用户某个菜单下拥有的所有权限
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="MenuId"></param>
        /// <returns></returns>
        public async Task<List<PermissionEntity>> GetTasAuthedListByMenuId(Guid UserId, int MenuId)
        {
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity, RolePermissionMapEntity, RoleEntity, TasUserRoleMapEntity>((p, rpm, r, urm) => new object[] {
                JoinType.Inner, p.Id == rpm.PermissionId
                ,JoinType.Inner, rpm.RoleId == r.Id
                ,JoinType.Inner, r.Id == urm.RoleId
            })
                .Where((p, rpm, r, urm) => urm.UserId == UserId && p.ParentId == MenuId && p.IsDeleted == 0 && r.IsDeleted == 0 && r.IsEnabled == (int)RoleIsEnabled.Enabled)
                .OrderBy((p, rpm, r, urm) => p.Sort, OrderByType.Desc)
                .Select((p, rpm, r, urm) => p).ToListAsync();
            return list;
        }

        /// <summary>
        /// 通过角色IdList获取权限列表
        /// </summary>
        /// <param name="roleIdList"></param>
        /// <returns></returns>
        public async Task<List<PermissionEntity>> GetTasListByRoleIdList(List<Guid> roleIdList)
        {
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity, RolePermissionMapEntity, RoleEntity, TasUserRoleMapEntity>((p, rpm, r, urm) => new object[] {
                JoinType.Inner, p.Id == rpm.PermissionId
                ,JoinType.Inner, rpm.RoleId == r.Id
                ,JoinType.Inner, r.Id == urm.RoleId
            })
               .Where((p, rpm, r, urm) => roleIdList.Contains(urm.RoleId) && p.IsDeleted == 0 && r.IsDeleted == 0 && r.IsEnabled == (int)RoleIsEnabled.Enabled)
                .OrderBy((p, rpm, r, urm) => p.Sort, OrderByType.Desc)
                .Select((p, rpm, r, urm) => p).ToListAsync();
            return list;
        }

        /// <summary>
        /// 获取Tas用户某个系统下拥有的所有权限
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="systemId"></param>
        /// <returns></returns>
        public async Task<List<PermissionListDto>> GetTasAuthedListBySystemId(Guid userId, int systemId)
        {
            //return Db.Queryable<EmployeePermission>().Where(p =>  p.IsDeleted == 0 && p.Id != SystemId).OrderBy(p => p.Sort, OrderByType.Desc).ToListAsync();
            var list = await _unitOfWork.QueryClient.Queryable<PermissionEntity, RolePermissionMapEntity, RoleEntity, TasUserRoleMapEntity>((p, rpm, r, urm) => new object[] {
                JoinType.Inner, p.Id == rpm.PermissionId
                ,JoinType.Inner, rpm.RoleId == r.Id
                ,JoinType.Inner, r.Id == urm.RoleId
            })
                .Where((p, rpm, r, urm) => urm.UserId == userId && p.SystemId == systemId && p.IsDeleted == 0 && r.IsDeleted == 0 && r.IsEnabled == (int)RoleIsEnabled.Enabled)
                .Select((p, rpm, r, urm) => new PermissionListDto()
                {
                    Id = p.Id,
                    ParentId = p.ParentId,
                    SystemId = p.SystemId,
                    SystemKey = p.SystemKey,
                    LevelIdx = p.LevelIdx,
                    LevelPath = p.LevelPath,
                    Name = p.Name,
                    PType = p.PType,
                    HasSub = p.HasSub,
                    AuthKey = p.AuthKey,
                    Sort = p.Sort,
                    Remark = p.Remark
                }).ToListAsync();
            return list;
        }
        #endregion
    }
}
